package com.example.music_mediaplayer;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.music_mediaplayer.fragment.BaiDuFragment;
import com.example.music_mediaplayer.fragment.KuGouFragment;
import com.example.music_mediaplayer.fragment.KuWoFragment;
import com.example.music_mediaplayer.fragment.LocationFragment;
import com.example.music_mediaplayer.fragment.QQFragment;
import com.example.music_mediaplayer.fragment.WangYiFragment;
import com.example.music_mediaplayer.api.BaseApi;
import com.example.music_mediaplayer.globalvariable.GlobalVariable;
import com.example.music_mediaplayer.model.JBase;
import com.example.music_mediaplayer.model.JsonBase;
import com.example.music_mediaplayer.model.MessageEvent;
import com.example.music_mediaplayer.model.SongsInfomation;
import com.example.music_mediaplayer.service.MyService;
import com.example.music_mediaplayer.tool.LogoutT;
import com.example.music_mediaplayer.tool.MsToTimeUtils;
import com.example.music_mediaplayer.tool.OkHttpTools;
import com.example.music_mediaplayer.tool.SharePreferenceTools;
import com.example.music_mediaplayer.tool.ToastTools;

import org.greenrobot.eventbus.EventBus;
import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements View.OnClickListener,BaseApi,SeekBar.OnSeekBarChangeListener{
    private View view1,view2,view3,view4,view5,view6,view7,view8;
    private EditText edt_search_song;
    private Button bu_search;
    private RelativeLayout rela_1,rela_2,rela_3,rela_4,rela_5,rela_6,rela_7,rela_8,rela_9,rela_10;
    private TextView te_wangyi;
    private TextView te_baidu;
    private TextView te_kuwo;
    private TextView te_qq;
//    private TextView te_xiami;
//    private TextView te_liebiao;
    private TextView te_kugou;
    private TextView te_location;
    private static TextView te_song_time_end;
    private static TextView te_song_time_start;
    private static TextView te_song_name;
    private static TextView te_song_artist;
    private static ImageView img_start_pause;
    private ImageView img_last;
    private ImageView img_next;
    private ImageView img_state;
    private ImageView img_song_photo;
    private int[] img_list = {R.drawable.random,R.drawable.single,R.drawable.list_play};
    private int currentState = 1;
    private WangYiFragment wangYiFragment;
    private BaiDuFragment baiDuFragment;
    private KuWoFragment kuWoFragment;
    private LocationFragment locationFragment;
//    private QQFragment qqFragment;
//    private XiaMiFragment xiaMiFragment;
//    private LieBiaoFragment lieBiaoFragment;
    private KuGouFragment kuGouFragment;
    private FragmentManager manager;
    private List<String> allIds = new ArrayList<>();
    private OkHttpTools okHttpTools = new OkHttpTools();
    private String information;
    private static Message msg;
    private Intent intent;
    private static SeekBar seekBar;
    private static int duration = 0;
    private static int currentTime = 0;
    private static int songTime = 0;
    private final static String MUSIC_PLAY_PAUSE = "music_play_pause";
    private final static String MUSIC_LAST = "music_last";
    private final static String MUSIC_NEXT = "music_next";

    public static Handler handler;
    private TextView textView;
    private static boolean flg = false;
    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("mainActivity","ssssss");
        initView();
        handler =  new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        bundle = msg.getData();
                        if (bundle.getString("change").equals("playing")){
                            img_start_pause.setImageResource(R.drawable.pause);
                            Log.i("001001","0000");
                        }else if(bundle.getString("change").equals("pause")){
                            if (!flg){
                                flg = true;
                            }else{
                                Log.i("001001","1111");
                                img_start_pause.setImageResource(R.drawable.start);
                            }
                        }
                        break;
                    case 2:
                        if (msg.arg1!=0){
                            seekBar.setMax(msg.arg1);
                            songTime = msg.arg1;
                            te_song_time_end.setText(MsToTimeUtils.stringForTime(msg.arg1));
                        }
                        currentTime = msg.arg2;
                        seekBar.setProgress(msg.arg2);
                        te_song_time_start.setText(MsToTimeUtils.stringForTime(msg.arg2));
                        break;
                    case 3:
                        bundle = msg.getData();
                        te_song_name.setText(bundle.getString("songName"));
                        te_song_artist.setText(bundle.getString("songArtist"));
                        Glide.with(MainActivity.this)
                                .load(bundle.getString("songPhoto"))
                                .placeholder(R.drawable.cd_photo)
                                .error(R.drawable.cd_photo)
                                .override(200,200)
                                .into(img_song_photo);
                        break;
                }
            }
        };
    }

    //此方法只是关闭软键盘
    private void hintKbTwo() {
        wangYiFragment.adapter.notifyDataSetChanged();
        InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
        if(imm.isActive()&&getCurrentFocus()!=null){
            if (getCurrentFocus().getWindowToken()!=null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    //此方法，如果显示则隐藏，如果隐藏则显示
    private void hintKbOne() {
        InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
// 得到InputMethodManager的实例
        if (imm.isActive()) {
            // 如果开启
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                    InputMethodManager.HIDE_NOT_ALWAYS);

        }
    }

    public void initView(){
        textView = (TextView) findViewById(R.id.text1);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setProgress(0);
        seekBar.setOnSeekBarChangeListener(this);
        te_song_time_end = (TextView) findViewById(R.id.song_time_end);
        te_song_time_start = (TextView) findViewById(R.id.song_time_start);
        te_song_name = (TextView) findViewById(R.id.song_name);
        te_song_name.setSelected(true);
        te_song_artist = (TextView) findViewById(R.id.artist);
        te_song_artist.setSelected(true);
        img_start_pause = (ImageView) findViewById(R.id.start_pause);
        img_start_pause.setOnClickListener(this);

        img_last = (ImageView) findViewById(R.id.last);
        img_last.setOnClickListener(this);
        img_next = (ImageView) findViewById(R.id.next);
        img_next.setOnClickListener(this);
        img_state = (ImageView) findViewById(R.id.play_state);
        img_state.setOnClickListener(this);
        img_song_photo = (ImageView) findViewById(R.id.song_photo);
        view1 = findViewById(R.id.view_wangyi);
        view2 = findViewById(R.id.view_baidu);
        view3 = findViewById(R.id.view_kuwo);
//        view4 = findViewById(R.id.view_xiami);
        view5 = findViewById(R.id.view_kugou);
//        view6 = findViewById(R.id.view_liebiao);
        view7 = findViewById(R.id.view_location);
//        view8 = findViewById(R.id.view_qq);
        edt_search_song = (EditText) findViewById(R.id.search_song);
        bu_search = (Button) findViewById(R.id.bu_search);
        bu_search.setOnClickListener(this);
        te_wangyi = (TextView) findViewById(R.id.wangyi);
        te_baidu = (TextView) findViewById(R.id.baidu);
        te_kuwo = (TextView) findViewById(R.id.kuwo);
//        te_xiami = (TextView) findViewById(R.id.xiami);
        te_kugou = (TextView) findViewById(R.id.kugou);
//        te_liebiao = (TextView) findViewById(R.id.liebiao);
        te_location = (TextView) findViewById(R.id.te_location);
//        te_qq = (TextView) findViewById(R.id.qq);
        rela_1 = (RelativeLayout) findViewById(R.id.rela_1);
        rela_1.setOnClickListener(this);
        rela_2 = (RelativeLayout) findViewById(R.id.rela_2);
        rela_2.setOnClickListener(this);
        rela_3 = (RelativeLayout) findViewById(R.id.rela_3);
        rela_3.setOnClickListener(this);
//        rela_4 = (RelativeLayout) findViewById(R.id.rela_4);
//        rela_4.setOnClickListener(this);
//        rela_5 = (RelativeLayout) findViewById(R.id.rela_5);
//        rela_5.setOnClickListener(this);
        rela_6 = (RelativeLayout) findViewById(R.id.rela_6);
        rela_6.setOnClickListener(this);
        rela_7 = (RelativeLayout) findViewById(R.id.to_playing);
        rela_7.setOnClickListener(this);
        rela_8 = (RelativeLayout) findViewById(R.id.rela_song);
        rela_8.setOnClickListener(this);
        rela_9 = (RelativeLayout) findViewById(R.id.rela_location);
        rela_9.setOnClickListener(this);
//        rela_10 = (RelativeLayout) findViewById(R.id.rela_10);
//        rela_10.setOnClickListener(this);
        setDefaultFrag();
        Log.i("songName",SharePreferenceTools.getInstance(MainActivity.this).getString("songName"));
        te_song_name.setText(SharePreferenceTools.getInstance(MainActivity.this).getString("songName"));
        te_song_artist.setText(SharePreferenceTools.getInstance(MainActivity.this).getString("songArtist"));
        Glide.with(MainActivity.this)
                .load(SharePreferenceTools.getInstance(MainActivity.this).getString("songPhoto"))
                .placeholder(R.drawable.cd_photo)
                .error(R.drawable.cd_photo)
                .override(200,200)
                .into(img_song_photo);
        if (!MyService.isPlaying()){
            img_start_pause.setImageResource(R.drawable.start);
        }else {
            if (SharePreferenceTools.getInstance(MainActivity.this).getString("playing").equals("playing")){
                img_start_pause.setImageResource(R.drawable.pause);
            }else if (SharePreferenceTools.getInstance(MainActivity.this).getString("playing").equals("pause")){
                img_start_pause.setImageResource(R.drawable.start);
            }
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (MyService.isPlaying()){
            img_start_pause.setImageResource(R.drawable.pause);
        }else {
            img_start_pause.setImageResource(R.drawable.start);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("onStart","mmmmmmm");
        te_song_name.setText(SharePreferenceTools.getInstance(MainActivity.this).getString("songName"));
        te_song_artist.setText(SharePreferenceTools.getInstance(MainActivity.this).getString("songArtist"));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("rererere","99999");
    }

    /**
     * 设置默认显示的Fragment
     */
    public void setDefaultFrag(){
        if (manager == null){
            manager = getFragmentManager();
        }
        FragmentTransaction ft = manager.beginTransaction();
        if (wangYiFragment == null){
            wangYiFragment = new WangYiFragment();
        }
        if (baiDuFragment == null){
            baiDuFragment = new BaiDuFragment();
        }
        if (kuWoFragment == null){
            kuWoFragment = new KuWoFragment();
        }
//        if (xiaMiFragment == null){
//            xiaMiFragment = new XiaMiFragment();
//        }
//        if (lieBiaoFragment == null){
//            lieBiaoFragment = new LieBiaoFragment();
//        }
        if (kuGouFragment == null){
            kuGouFragment = new KuGouFragment();
        }
        if (locationFragment == null){
            locationFragment = new LocationFragment();
        }
//        if (qqFragment == null){
//            qqFragment = new QQFragment();
//        }
        ft.add(R.id.list_fram,wangYiFragment)
                .add(R.id.list_fram,baiDuFragment)
                .add(R.id.list_fram,kuWoFragment)
//                .add(R.id.list_fram,xiaMiFragment)
//                .add(R.id.list_fram,lieBiaoFragment)
                .add(R.id.list_fram,kuGouFragment)
                .add(R.id.list_fram,locationFragment)
//                .add(R.id.list_fram,qqFragment)
//                .hide(lieBiaoFragment)
                .hide(baiDuFragment)
                .hide(kuWoFragment)
//                .hide(xiaMiFragment)
                .hide(kuGouFragment)
                .hide(locationFragment)
//                .hide(qqFragment)
                .show(wangYiFragment)
                .commit();
    }

    /**
     * 虾米音乐抓取搜索音乐id列表
     * @param input
     */
    public void getXiaMiSongsIdList(String input)  {
        Document document = null;
        try {
            document = Jsoup.connect(ApiInfo.xiaMiSearch + input).get();//jsoup连接最终拼接而成的请求字符串
//            LogoutT.logout("document  "+document);
//            document = Jsoup.connect("http://musicmini.baidu.com/app/search/searchList.php?ie=utf-8&page=1&qword="+input).get();
            Elements elements = document.getElementsByClass("track_list");//选择类标签
            if (elements.size()!=0){
                Elements all = elements.get(0).getElementsByClass("chkbox");
                int size = all.size();
                for (int i = 0 ; i<size ; i++){
                    String id = all.get(i).select("input").attr("value");
                    if (!id.equals("")){
                        allIds.add(id);//不为空的话加入list中，便于初次抓取完以后统一请求
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取百度歌曲列表
     * @param input
     */
    public void getBaiDuSongList(String input){
        GlobalVariable.BaiDuSongList.clear();
        Document document = null;
        try {
            document = Jsoup.connect(ApiInfo.baiDuSearch + input).get();
            Element element = document.getElementById("sc-table");
//            LogoutT.logout(element+"00000000");
            if (element!=null){
                Elements all = element.getElementsByClass("box");
//                LogoutT.logout(all.size()+"1111111");
                for (int i = 0 ; i<all.size();i++){
                    String id = all.get(i).select("a").attr("onclick");
                    if (!id.equals("")&&id!=null){
//                        LogoutT.logout(id);
                        String[] str = id.split("\'");
                        SongsInfomation s = new SongsInfomation();
                        for (int j = 0 ;j < str.length ; j++){
                            if (j == 1){
                                s.setId(str[j]);
                            }else if(j==3){
                                s.setSongArtist(str[j]);
                            }else if (j==5){
                                s.setSongName(str[j]);
                            }
                        }
                        GlobalVariable.BaiDuSongList.add(s);
                    }
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 虾米音乐根据歌曲id获取歌曲url
     * @param allIds
     * @return
     */
    public List<SongsInfomation> getXiaMiSongUrl(List<String> allIds){
//        getXiaMiSongsIdList(input);
        LogoutT.logout(allIds.size()+"size");
        int idSize = allIds.size();
        LogoutT.logout(idSize+"");
//        String path = StringUtils.decodeMusicUrl("6hAFxn114F%613_5mae57%Ed46Ed9t%mie9912557553puy115-bb534at21at7691E279963t%66E%9772d3pF2m%%47%2%5%7_%h3%%-56f2d2f%%8i228%582758l3_D55%Ee7%c632..FF92E5F1E9.Fk1EE5-f1518");
//        LogoutT.logout("path  "+path);
        for (int i = 0 ; i<idSize ; i++){
            String postUrl = ApiInfo.xiaMiSongUrl + allIds.get(i);
//            LogoutT.logout(postUrl);
            try {
                LogoutT.logout("11111");
                Document d = Jsoup.connect("http://www.xiami.com/song/1775713592")
                        .data("query","Java")
                        .userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                        .cookie("auth","token")
                        .timeout(3000)
                        .get();//连接相应Id的接口地址
                LogoutT.logout("22222"+d);
                Elements element = d.select("div.cd2play");
                LogoutT.logout("33333"+element.size());
//                %7B%22LOW%22%3A%22FREE%22%2C%22HIGH%22%3A%22FREE%22%7D
                for ( int j = 0 ;j<element.size();j++){
                    Elements urls = element.get(j).getElementsByTag("a");
                    String a = urls.get(j).attr("href");
                    String b = urls.get(j).text();
                    LogoutT.logout(a+"    aaaaaaa");
                    LogoutT.logout(b+"    bbbbbbb");
                }
//                for (Element e : element){
//                    Elements s = e.getElementsByTag("a");
//                    String a = e.text();
//                    LogoutT.logout(a+"aaaaaa");
////                    SongsInfomation songsInfomation = new SongsInfomation();
////                    songsInfomation.setId(allIds.get(i));
////                    songsInfomation.setSongName(e.select("title").text());
////                    LogoutT.logout(e.select("title").text()+"title");
////                    songsInfomation.setSongArtist(e.select("artist").text());
////                    LogoutT.logout(e.select("artist").text()+"artist");
////                    songsInfomation.setSongPath(e.select("location").text());
////                    LogoutT.logout(e.select("location").text()+"location");
////                    GlobalVariable.XiaMiSongList.add(songsInfomation);
//                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return GlobalVariable.XiaMiSongList;
    }

    public void changeFragment(Fragment f1,Fragment f2,Fragment f3,Fragment f4,Fragment f5){
        if (manager == null){
            manager = getFragmentManager();
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.hide(f1).hide(f2).hide(f3).hide(f4).show(f5).commit();
    }

    public void changeTextColor(TextView t1,TextView t2,TextView t3,TextView t4,TextView t5){
        t1.setTextColor(getResources().getColor(R.color.red));
        t2.setTextColor(getResources().getColor(R.color.colorblack));
        t3.setTextColor(getResources().getColor(R.color.colorblack));
        t4.setTextColor(getResources().getColor(R.color.colorblack));
        t5.setTextColor(getResources().getColor(R.color.colorblack));
//        t6.setTextColor(getResources().getColor(R.color.colorblack));
//        t6.setTextColor(getResources().getColor(R.color.colorblack));
//        t6.setTextColor(getResources().getColor(R.color.colorblack));
    }

    public void changeView(View v1 , View v2 , View v3 , View v4 , View v5){
        v1.setVisibility(View.VISIBLE);
        v2.setVisibility(View.INVISIBLE);
        v3.setVisibility(View.INVISIBLE);
        v4.setVisibility(View.INVISIBLE);
        v5.setVisibility(View.INVISIBLE);
//        v6.setVisibility(View.INVISIBLE);
//        v6.setVisibility(View.INVISIBLE);
//        v6.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rela_1:
                changeFragment(baiDuFragment,kuWoFragment,kuGouFragment,locationFragment,wangYiFragment);
                changeTextColor(te_wangyi,te_baidu,te_kuwo,te_kugou,te_location);
                changeView(view1,view2,view3,view5,view7);
                break;
            case R.id.rela_2:
                changeFragment(wangYiFragment,kuWoFragment,kuGouFragment,locationFragment,baiDuFragment);
                changeTextColor(te_baidu,te_wangyi,te_kuwo,te_kugou,te_location);
                changeView(view2,view1,view3,view5,view7);
                break;
            case R.id.rela_3:
                changeFragment(wangYiFragment,baiDuFragment,kuGouFragment,locationFragment,kuWoFragment);
                changeTextColor(te_kuwo,te_wangyi,te_baidu,te_kugou,te_location);
                changeView(view3,view2,view1,view5,view7);
                break;
//            case R.id.rela_4:
//                changeFragment(wangYiFragment,baiDuFragment,kuWoFragment,lieBiaoFragment,kuGouFragment);
//                changeTextColor(te_xiami,te_wangyi,te_baidu,te_kuwo,te_liebiao,te_kugou);
//                changeView(view4,view2,view3,view1,view5,view6);
//                break;
//            case R.id.rela_5:
//                changeFragment(wangYiFragment,baiDuFragment,kuWoFragment,kuGouFragment,locationFragment,lieBiaoFragment);
//                changeTextColor(te_liebiao,te_wangyi,te_baidu,te_kuwo,te_kugou,te_liebiao);
//                changeView(view6,view2,view3,view1,view5,view7);
//                lieBiaoFragment.adapter.notifyDataSetChanged();
//                break;
            case R.id.rela_6:
                changeFragment(wangYiFragment,baiDuFragment,kuWoFragment,locationFragment,kuGouFragment);
                changeTextColor(te_kugou,te_baidu,te_kuwo,te_wangyi,te_location);
                changeView(view5,view2,view3,view1,view7);
                break;
//            case R.id.rela_10:
//                changeFragment(wangYiFragment,baiDuFragment,kuWoFragment,locationFragment,qqFragment,kuGouFragment);
////                changeTextColor(te_qq,te_kugou,te_baidu,te_kuwo,te_wangyi,te_location);
//                changeView(view8,view5,view2,view3,view1,view7);
//                break;
            case R.id.rela_location:
                changeFragment(wangYiFragment,baiDuFragment,kuWoFragment,kuGouFragment,locationFragment);
                changeTextColor(te_location,te_kugou,te_baidu,te_kuwo,te_wangyi);
                changeView(view7,view5,view2,view3,view1);
                break;
            case R.id.to_playing:
                if (GlobalVariable.LocationSongList.size()==0){
                    ToastTools.toastMsg(this,"当前没歌曲在播放");
                }else{
                    intent = new Intent(this,PlayingActivity.class);
                    intent.putExtra("song_name",te_song_name.getText().toString());
                    intent.putExtra("song_artist",te_song_artist.getText().toString());
                    intent.putExtra("seekBar",seekBar.getMax());
                    intent.putExtra("songLrc",MyService.getSongLrc());
                    intent.putExtra("songTime",songTime);
                    intent.putExtra("currentTime",currentTime);
                    startActivity(intent);
                }
                break;
            case R.id.rela_song:
                if (GlobalVariable.LocationSongList.size()==0){
                    ToastTools.toastMsg(this,"当前没歌曲在播放");
                }else{
                    intent = new Intent(this,PlayingActivity.class);
                    intent.putExtra("song_name",te_song_name.getText().toString());
                    intent.putExtra("song_artist",te_song_artist.getText().toString());
                    intent.putExtra("seekBar",seekBar.getMax());
                    intent.putExtra("songLrc",MyService.getSongLrc());
                    intent.putExtra("songTime",songTime);
                    intent.putExtra("currentTime",currentTime);
                    startActivity(intent);
                }
                break;
            case R.id.bu_search:
                final String songName = edt_search_song.getText().toString();
                if (songName.equals("")){
                    ToastTools.toastMsg(this,"请输入关键词");
                }else{
                    okHttpTools.wangYiSearchSong(this,"search","1",songName,1);
                    okHttpTools.kuGouSearchSong(this,songName,"1","60",2);
                    okHttpTools.kuWoSearchSong(this,songName,"1","60",3);
                    okHttpTools.QQSearchSong(this,songName,"1","60",4);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            getBaiDuSongList(songName);
                        }
                    }).start();
                    hintKbTwo();
                    Handler handler = new Handler();
                    textView.setVisibility(View.VISIBLE);
                    textView.setText("玩命加载中...");
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (GlobalVariable.WangYiSongList.size()!=0||GlobalVariable.BaiDuSongList.size()!=0||GlobalVariable.KuWoSongList.size()!=0||GlobalVariable.KuGouSongList.size()!=0){
                                LogoutT.logout("kkkkkkkk");
                                wangYiFragment.adapter.notifyDataSetChanged();
                                baiDuFragment.adapter.notifyDataSetChanged();
                                kuWoFragment.adapter.notifyDataSetChanged();
                                kuGouFragment.adapter.notifyDataSetChanged();
                                textView.setVisibility(View.GONE);
                            }
                        }
                    },3000);
                }
                break;
            case R.id.start_pause:
                img_start_pause.setImageResource(R.drawable.pause);
                if (GlobalVariable.LocationSongList.size()==0){
                    ToastTools.toastMsg(this,"当前列表为空");
                }else{
                    information = MUSIC_PLAY_PAUSE;
                    intent = new Intent(this, MyService.class);
                    intent.putExtra("msg",information);
                    intent.putExtra("url","");
                    intent.putExtra("songName","");
                    intent.putExtra("songArtist","");
                    intent.putExtra("songPhoto","");
                    startService(intent);
                }
                break;
            case R.id.last:
                if (GlobalVariable.LocationSongList.size()==0){
                    ToastTools.toastMsg(this,"当前没有歌曲");
                }else{
                    information = MUSIC_LAST;
                    intent = new Intent(this,MyService.class);
                    intent.putExtra("msg",information);
                    intent.putExtra("url","");
                    intent.putExtra("songName","");
                    intent.putExtra("songArtist","");
                    intent.putExtra("songPhoto","");
                    startService(intent);
                }
                break;
            case R.id.next:
                if (GlobalVariable.LocationSongList.size()==0){
                    ToastTools.toastMsg(this,"当前没有歌曲");
                }else{
                    information = MUSIC_NEXT;
                    intent = new Intent(this,MyService.class);
                    intent.putExtra("msg",information);
                    intent.putExtra("url","");
                    intent.putExtra("songName","");
                    intent.putExtra("songArtist","");
                    intent.putExtra("songPhoto","");
                    startService(intent);
                }
                break;
            case R.id.play_state:
                currentState = currentState%img_list.length;
                currentState++;
                if (currentState == 1){
                    ToastTools.toastMsg(this,"随机播放");
                    img_state.setImageResource(R.drawable.random);
                    intent = new Intent();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        //  大于等于24即为7.0及以上执行内容
                        intent.setComponent(new ComponentName("com.example.music_mediaplayer","com.example.music_mediaplayer.broadcast.ServiceReceiver"));
                    }
                    intent.setAction("play_state");
                    intent.putExtra("state","1");
                    sendBroadcast(intent);
                }else if(currentState == 2){
                    ToastTools.toastMsg(this,"列表播放");
                    img_state.setImageResource(R.drawable.list_play);
                    intent = new Intent();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        //  大于等于24即为7.0及以上执行内容
                        intent.setComponent(new ComponentName("com.example.music_mediaplayer","com.example.music_mediaplayer.broadcast.ServiceReceiver"));
                    }
                    intent.setAction("play_state");
                    intent.putExtra("state","2");
                    sendBroadcast(intent);
                }else if (currentState == 3){
                    ToastTools.toastMsg(this,"单曲循环");
                    img_state.setImageResource(R.drawable.single);
                    intent = new Intent();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        //  大于等于24即为7.0及以上执行内容
                        intent.setComponent(new ComponentName("com.example.music_mediaplayer","com.example.music_mediaplayer.broadcast.ServiceReceiver"));
                    }
                    intent.setAction("play_state");
                    intent.putExtra("state","3");
                    sendBroadcast(intent);
                }
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser){
            int currentProgress = seekBar.getProgress();
            intent = new Intent();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //  大于等于24即为7.0及以上执行内容
                intent.setComponent(new ComponentName("com.example.music_mediaplayer","com.example.music_mediaplayer.broadcast.ServiceReceiver"));
            }
            intent.setAction("seekBarChange");
            intent.putExtra("currentProgress",currentProgress);
            sendBroadcast(intent);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase ,int flg) {
        if (flg == 1){
            GlobalVariable.WangYiSongList.clear();
            if (jsonBase.getResult().getSongs().size()!=0){
                for (int i = 0 ; i<jsonBase.getResult().getSongs().size();i++){
                    SongsInfomation songsInfomation = new SongsInfomation();
                    songsInfomation.setSongName(jsonBase.getResult().getSongs().get(i).getName().replace("&nbsp",""));
                    songsInfomation.setSongPhoto(jsonBase.getResult().getSongs().get(i).getAl().getPicUrl());
                    songsInfomation.setFlg(false);
                    songsInfomation.setId(jsonBase.getResult().getSongs().get(i).getId());
                    for (int j =0 ;j<jsonBase.getResult().getSongs().get(i).getAr().size();j++){
                        songsInfomation.setSongArtist(jsonBase.getResult().getSongs().get(i).getAr().get(j).getName());
                    }
                    GlobalVariable.WangYiSongList.add(songsInfomation);
                }
            }
        }else if(flg == 2){

        }else if (flg ==3){
            GlobalVariable.KuWoSongList.clear();
            if (jsonBase.getAbslist().size()!=0){
                for (int i = 0 ;i<jsonBase.getAbslist().size();i++){
                    SongsInfomation songsInfomation = new SongsInfomation();
                    songsInfomation.setFlg(false);
                    songsInfomation.setId(jsonBase.getAbslist().get(i).getMUSICRID());
                    songsInfomation.setSongArtist(jsonBase.getAbslist().get(i).getARTIST());
                    songsInfomation.setSongName(jsonBase.getAbslist().get(i).getSONGNAME().replace("&nbsp",""));
                    GlobalVariable.KuWoSongList.add(songsInfomation);
                }
            }
        }else if(flg == 4){

        }

    }

    @Override
    public void backMsg(String success, JBase jBase, int flg) {
        if (flg == 2){
            GlobalVariable.KuGouSongList.clear();
            if (jBase.getData().getInfo().size()!=0){
                for (int i = 0;i<jBase.getData().getInfo().size();i++){
                    SongsInfomation songsInfomation = new SongsInfomation();
                    songsInfomation.setFlg(false);
                    songsInfomation.setSongName(jBase.getData().getInfo().get(i).getSongname().replace("&nbsp",""));
                    songsInfomation.setSongArtist(jBase.getData().getInfo().get(i).getSingername());
                    songsInfomation.setId(jBase.getData().getInfo().get(i).getHash());
                    GlobalVariable.KuGouSongList.add(songsInfomation);
                }
            }
        }
    }

    /**
     * 按back键程序退到后台运行，不销毁
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
